## filter snp adjacent in VCF files, suppose vcf file is sorted and in same chr
## author: Yaping Liu  lyping1986@gmail.com

my $file = $ARGV[0];
my $filter_space = $ARGV[1]; #how far away snps should be filtered out..

my $use_age = "USAGE: perl filter_snp_adjacent_in_vcf.pl input_sorted_vcf distance_to_filer";
if($file eq ""){
	print "$use_age\n";
	exit(1);
}

my $output_file_name = $file.".filter.vcf";
my $output_file_name_filtered_snp = $file.".filteredSNPs.vcf";
my @out;
my @out_filtered;
my $filtered_num = 0;
my $total_num=0;
my $rest_num = 0;
open(FH,"<$file") or die;


open(OUT,">$output_file_name") or die;
open(OUT_FILTER,">$output_file_name_filtered_snp") or die;
my $preLoc = 0;
my %preLoc_need_delete = ();
my $preContig="chr1";
my @temp;
my @temp_location;
while(<FH>){
	my $line=$_;
	chomp($line);
	if($line =~ /^#/){
		print OUT "$line\n";
		next;
	}
	my @splitin = split "\t",$line;
	if($splitin[0] ne $preContig){
		$preContig = $splitin[0];
		foreach my $tmpLine(@temp){
			print OUT "$tmpLine\n";
			$rest_num++;
		}
		#print OUT "$line\n";
		$preLoc = 0;
		for (keys %preLoc_need_delete)
    	{
        	delete $preLoc_need_delete{$_};
    	}
		
		@temp=();
		@temp_location=();
		push(@temp,$line);
		push(@temp_location,$splitin[1]);
	}
	else{
		if($#temp<10){
			if($splitin[9] !~ /0\/0/){
				if($splitin[1] - $preLoc <= $filter_space){
					$preLoc_need_delete{$preLoc}=1;
					$preLoc = $splitin[1];
					$filtered_num++;	
					print OUT_FILTER "$line\n";
				}
				else{
					$preLoc = $splitin[1];
					push(@temp,$line);
					push(@temp_location,$splitin[1]);
				}
			}
			else{
				
				push(@temp,$line);
				push(@temp_location,$splitin[1]);

			}
		}
		else{
			if($splitin[9] !~ /0\/0/){
				if($splitin[1] - $preLoc <= $filter_space){
					my $tmpLine=shift(@temp);
					my $tmp_location=shift(@temp_location);
					if(exists $preLoc_need_delete{$tmp_location}){
						$filtered_num++;
						delete $preLoc_need_delete{$tmp_location};
						print OUT_FILTER "$tmpLine\n";
					}
					else{
						
						print OUT "$tmpLine\n";
						$rest_num++;
					}
					$preLoc_need_delete{$preLoc}=1;
					$preLoc = $splitin[1];
					$filtered_num++;	
					print OUT_FILTER "$line\n";
				}
				else{
					$preLoc = $splitin[1];
					my $tmpLine=shift(@temp);
					my $tmp_location=shift(@temp_location);
					if(exists $preLoc_need_delete{$tmp_location}){
						$filtered_num++;
						delete $preLoc_need_delete{$tmp_location};
						print OUT_FILTER "$tmpLine\n";
					}
					else{
						
						print OUT "$tmpLine\n";
						$rest_num++;
					}
					
					push(@temp,$line);
					push(@temp_location,$splitin[1]);
					
				
				}
			}
			else{
					my $tmpLine=shift(@temp);
					my $tmp_location=shift(@temp_location);
					if(exists $preLoc_need_delete{$tmp_location}){
						$filtered_num++;
						delete $preLoc_need_delete{$tmp_location};
						print OUT_FILTER "$tmpLine\n";
					}
					else{
						
						print OUT "$tmpLine\n";
						$rest_num++;
					}
					push(@temp,$line);
					push(@temp_location,$splitin[1]);
				
			
			}
		}
		
		
	}
	#print $#temp;
	#print "\n";
	$total_num++;
}
foreach my $tmpLine(@temp){
		print OUT "$tmpLine\n";
		$rest_num++;
}
close(FH);
close(OUT);
close(OUT_FILTER);
#my $rest=$total_num-$filtered_num;
print "total SNPs: $total_num\nFiltered SNPs: $filtered_num\nRest of SNPs: $rest_num\n";
